package com.example.laixunlong.control;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.example.laixunlong.R;

public class BoxRadioSwitch_xuankuang extends AppCompatActivity {

    CheckBox che1;
    CheckBox che2;
    Switch swi1;
    TextView txt1,radio_text_1;
    RadioGroup rad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boxradioswitch_xuankuang);
        che1=findViewById(R.id.checkbox_1);
        che2=findViewById(R.id.checkbox_2);
        /*设置状态监听*/
        che1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override/*回调函数中的两个参数分别为点击的实例,当前状态*/
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                che1.setText((isChecked?"":"未")+"选中");
                che2.setChecked(isChecked);
            }
        });
        /*复选框和开关通用一种监听事件*/
        swi1=findViewById(R.id.switch_1);
        txt1=findViewById(R.id.switch_text_1);
        swi1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {txt1.setText("开关已打开");txt1.setTextColor(Color.parseColor("#00AEFF"));}
                else
                {txt1.setText("开关已关闭");txt1.setTextColor(Color.parseColor("#818181"));}

            }
        });
        /*单选框和复选框通用一种监听事件*/
        rad=findViewById(R.id.radio_1);
        radio_text_1=findViewById(R.id.radio_text_1);
        rad.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.xuan_1:
                        radio_text_1.setText("选中第一个选项");
                        break;
                    case R.id.xuan_2:
                        radio_text_1.setText("选中第二个选项");
                        break;
                    case R.id.xuan_3:
                        radio_text_1.setText("选中第三个选项");
                        break;
                }
            }
        });
    }
}